<template>
<div>
  <el-card>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>旅游路线管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-row  style="margin-top: 10px">
      <el-button type="primary" @click="openAddNewDialog()"  style="margin-left: 10px">新增</el-button>
      <download-excel
          style="float: left"
          class="export-excel-wrapper"
          :data="roleData"
          type="xls"
          worksheet="worksheet"
          name="用户信息">
        <el-button type="primary">导出</el-button>
      </download-excel>
    </el-row>
    <el-table
        :data="routeData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="项目编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="项目名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="days"
          label="天数"
          width="180">
      </el-table-column>
      <el-table-column
          prop="brief"
          label=摘要>
      </el-table-column>
      <el-table-column
          prop="price"
          label="价格">
      </el-table-column>
      <el-table-column label="操作"  width="200px"
                       align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 修改数据的表单 -->
  <el-dialog title="编辑路线数据" :visible.sync="editFormVisible">
    <el-form :model="editForm"  label-width="120px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="天数" prop="days">
        <el-input v-model="editForm.days"></el-input>
      </el-form-item>
      <el-form-item label="摘要" prop="brief">
        <el-input v-model="editForm.brief"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="editForm.price"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="editFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleEdit()">确 定</el-button>
    </div>
  </el-dialog>
  <!-- 新增数据的表单 -->
  <el-dialog title="新增路线数据" :visible.sync="FormVisible">
    <el-form :model="editForm"  label-width="120px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="天数" prop="days">
        <el-input v-model="editForm.days"></el-input>
      </el-form-item>
      <el-form-item label="摘要" prop="brief">
        <el-input v-model="editForm.brief"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="editForm.price"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="FormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addSpot()">确 定</el-button>
    </div>
  </el-dialog>
  <el-pagination
      @current-change="changePage"
      layout="total, prev, pager, next"
      :hide-on-single-page="true"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total">
  </el-pagination>
</div>
</template>

<script>
export default {
  name: "Roue",
  data:function () {
    return {
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: '',
      total:'',
      categoryDataOptions:'',
      editFormVisible: false,
      FormVisible: false,
      routeData: [],
      editForm: {
        id: '',
        name: '',
        days:'',
        brief: '',
        price:'',
      },
    }
  },
  methods:{
    // 弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:8080/v1/routes/' +tableItem.id;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log('jsonResult ='+ jsonResult)
        if (jsonResult.code == 1) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 执行修改
    handleEdit() {
      let url = 'http://localhost:8080/v1/routes/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '修改路线成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadRouteList();
        } else if (jsonResult.state == 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 打开删除确认框
    openDeleteConfirm(role) {
      let message = '此操作将永久删除【' + role.name + '】景点，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(role);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(route) {
      let url = 'http://localhost:8080/v1/routes/' + route.id + '/deleteRoute';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '删除【' + route.name + '】景点成功！',
            type: 'success'
          });
          this.loadRouteList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    // 打开新增对话框
    openAddNewDialog(tableItem) {
      this.FormVisible = true
    },
    // 执行新增
    addSpot() {
      let url = 'http://localhost:8080/v1/routes/add-new';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '新增路线成功！',
            type: 'success'

          });
          this.FormVisible = false;
          this.loadRouteList();
        } else if (jsonResult.code== 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.FormVisible = false;
            }
          });
        } else {
          let title = '操作失败,新增的路线已存在!';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    roleHandleEdit(index,user){
      console.log(index);
    },
    roleHandleDelete(){
      console.log(index);
    },
    changePage(page) {
      this.$router.replace('?page=' + page);
      this.loadRouteList();
    },
    loadRouteList(){
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:8080/v1/routes/list?page='+page;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response)=>{
        let jsonResult = response.data;
        if(response.data.code==1){
          this.routeData = jsonResult.data.list;
          this.currentNoPassPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
          console.log(this.routeData)
        }
      })
    },

  },
  mounted() {
    this.loadRouteList();
  }
}
</script>

<style scoped>

</style>